package com.ly.blog2Word.service.impl;

import com.ly.blog2Common.core.page.MybatisPageHelper;
import com.ly.blog2Common.core.page.PageRequest;
import com.ly.blog2Common.core.page.PageResult;
import com.ly.blog2Common.utils.ObjectUtils;
import com.ly.blog2Word.dao.WordArticleMapper;
import com.ly.blog2Word.dao.model.WordArticle;
import com.ly.blog2Word.service.WordArticleService;
import com.ly.blog2Word.utils.ArticleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: lyMaster
 * @time: 2020/7/17 19:07
 */
@Service
public class WordArticleImpl implements WordArticleService {

    @Autowired
    private WordArticleMapper wordArticleMapper;

    @Override
    public int save(WordArticle record) {
        if(record.getId() == null || record.getId() == 0) {
            return wordArticleMapper.insertSelective(record);
        }
        return wordArticleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(WordArticle record) {
        return wordArticleMapper.deleteByPrimaryKey(record.getId());
    }

    @Override
    public int delete(List<WordArticle> records) {
        for(WordArticle record:records) {
            delete(record);
        }
        return 1;
    }

    @Override
    public WordArticle findById(Long id) {
        WordArticle wordArticle = wordArticleMapper.selectByPrimaryKey(id);
        wordArticle.setWatchTime(wordArticle.getWatchTime()+1);
        save(wordArticle);
        return wordArticle;
    }

    @Override
    public Map<String,Object> findAddLabelById(Long id) {
        Map<String,Object> resp = wordArticleMapper.selectAddLabelByPrimaryKey(id);
        WordArticle wordArticle = null;
        try {
            wordArticle = ObjectUtils.mapToObject(resp,WordArticle.class);
            wordArticle.setWatchTime(wordArticle.getWatchTime()+1);
            save(wordArticle);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return resp;
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
//        return MybatisPageHelper.findPage(pageRequest, wordArticleMapper);
        PageResult pageResult = MybatisPageHelper.findPage(pageRequest, wordArticleMapper,"findPageWithMap");
        return pageResult;
    }

    @Override
    public PageResult findPageShow(PageRequest pageRequest) {
//        return MybatisPageHelper.findPage(pageRequest, wordArticleMapper);
        PageResult pageResult = MybatisPageHelper.findPage(pageRequest, wordArticleMapper,"findPageWithMap");
        //处理文章内容数据
        List<?> articleList = pageResult.getContent();
        for(Object o:articleList){
            if(o instanceof WordArticle){
                WordArticle wordArticle = (WordArticle) o;
                String content = wordArticle.getContent();
                content = ArticleUtils.cleanHtmlAndLimitLength(content);
                wordArticle.setContent(content);
            }
            if(o instanceof Map){
                Map<String,String> wordArticle = (Map<String,String>) o;
                String content = wordArticle.get("content");
                content = ArticleUtils.cleanHtmlAndLimitLength(content);
                wordArticle.put("content",content);
            }
        }

        return pageResult;
    }

}
